package aps

import org.apache.commons.logging.LogFactory
import org.json.JSONObject

/**
 * WaferSaw物料专用controller
 */
class WaferSawController {

    def dataSource

    private final static logger = LogFactory.getLog(this)

    def index() {

        print dataSource
        def standardJSONForPDA = JSONObject.newInstance()
        standardJSONForPDA.put("code", "200")
        standardJSONForPDA.put("message", "")
        standardJSONForPDA.put("data", null)

        def SeqNo = params.SeqNo ?: ''
        def Materialtype = params.Materialtype ?: ''

        def db
        def sql
        try {
            if (Materialtype == 'HubBlade' || Materialtype == 'HubBladeYZ1' || Materialtype == 'HubBladeYZ2') {
                //硬刀
                sql = "SELECT DISTINCT o.MATERIALNO MaterialPartNo,o. CUTTEREXPOSE SawLength,o.CUTTERWIDTH SawThickness,o.MATERIALLOTNO SeqNo FROM EAP.aps_materialinfo　o where o.Materialtype='${Materialtype}' and o.MATERIALLOTNO='${SeqNo}'"
            } else if (Materialtype == 'HublessBlade') {
                //软刀
                sql = "SELECT DISTINCT o.MATERIALNO MaterialPartNo,o. CUTTERINNERDIAMETER InternalDiameter,o.CUTTEROUTERDIAMETER ExternalDiameter,o.CUTTERWIDTH SawThickness,o.MATERIALLOTNO SeqNo FROM EAP.aps_materialinfo　o where o.Materialtype='${Materialtype}' and o.MATERIALLOTNO='${SeqNo}'"
            }
            db = new groovy.sql.Sql(dataSource)
            def list = db.rows(sql)

            if (list) {
                standardJSONForPDA.put("data", list.get(0))
            } else {
                standardJSONForPDA.put("code", "-200")
                standardJSONForPDA.put("message", "暂无物料信息!")
            }
            logger.info("WaferSaw:${list}")
        } catch (Exception e) {
            standardJSONForPDA.put("code", "-200")
            standardJSONForPDA.put("message", "查询数据库异常!")
            logger.error(e.toString())
        } finally {
            if (db) {
                db.close();
            }
            render standardJSONForPDA.toString()
        }
    }
}